/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.user.controller.assembler.UserPostAssembler;
import net.siufung.market.base.user.controller.model.dto.UserPostDto;
import net.siufung.market.base.user.controller.model.dto.UserPostSaveDto;
import net.siufung.market.base.user.controller.model.dto.UserPostUpdateDto;
import net.siufung.market.base.user.controller.model.dto.page.UserPostPageDto;
import net.siufung.market.base.user.controller.model.vo.UserPostVo;
import net.siufung.market.base.user.controller.regulation.UserPostCreateRegulation;
import net.siufung.market.base.user.controller.regulation.UserPostRemoveRegulation;
import net.siufung.market.base.user.controller.regulation.UserPostUpdateRegulation;
import net.siufung.market.base.user.mapper.IUserMapper;
import net.siufung.market.base.user.mapper.IUserPostMapper;
import net.siufung.market.base.user.mapper.entity.UserPost;
import net.siufung.market.base.user.service.IUserPostService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统岗位 前端控制器
 *
 * @author 陈建峰
 * @date 2023-02-06 23:25:52
 * @since v1.0.0
 */
@Api(tags = "系统岗位")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/user/post")
public class UserPostController {

    private final IUserPostMapper userPostMapper;
    private final IUserPostService userPostService;
    private final IUserMapper userMapper;


    /**
     * 通过主键获取 系统岗位
     * @param id 主键
     * @return Result<UserPostVo>
     * */
    @ApiOperation(value = "通过Id获取系统岗位信息")
    @GetMapping(value = "/id/{id}")
    public Result<UserPostVo> getById(
            @PathVariable("id") Integer id) {
        return Result.ok(
            UserPostAssembler.from(userPostMapper.selectById(id)));
    }

    /**
     * 获取 系统岗位列表
     * @param userPostDto 属性
     * @return Result<List<UserPostVo>>
     * */
    @ApiOperation(value = "获取系统岗位列表")
    @PostMapping(value = "/list")
    public Result<List<UserPostVo>> list(
            @RequestBody UserPostDto userPostDto) {
        return Result.ok(
            UserPostAssembler.fromList(userPostMapper.selectList(Wrappers.query(userPostDto.to()))));
    }

    /**
     * 获取 系统岗位分页
     * @param userPostPageDto 属性
     * @return 查询分页结果
     * */
    @ApiOperation(value = "获取系统岗位分页")
    @PostMapping(value="/page")
    public Result<PagerResult<UserPostVo>> getPage(
            @RequestBody UserPostPageDto userPostPageDto) {
        userPostPageDto.validated();
        QueryWrapper<UserPost> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(!Strings.isNullOrEmpty(userPostPageDto.getPostName()),
                UserPost::getPostName, userPostPageDto.getPostName());
        wrapper.lambda().orderByDesc(UserPost::getCreatedAt);
        IPage<UserPost> userPostPage = userPostMapper.selectPage(
                new Page<>(userPostPageDto.getPageNo(), userPostPageDto.getPageSize()), wrapper);
        PagerResult<UserPostVo> userPostVoPage = new PagerResult<>(
                userPostPage.getCurrent(), userPostPage.getSize(), userPostPage.getTotal());
        userPostVoPage.setRecords(
            UserPostAssembler.fromList(userPostPage.getRecords()));
        return Result.ok(userPostVoPage);
    }

    /**
     * 通过主键删除 系统岗位 对象
     * @param id 主键
     * @return 删除结果
     * */
    @ApiOperation(value = "删除系统岗位对象")
    @PostMapping(value="/remove/{id}")
    public Result<Void> removeById(
        @PathVariable("id") Integer id) {
        UserPostRemoveRegulation userPostRemoveRegulation = new UserPostRemoveRegulation(userMapper);
        userPostRemoveRegulation.isSatisfiedBy(id);
        if(userPostService.removeById(id)){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 创建 系统岗位
     * @param userPostSaveDto 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "创建 系统岗位")
    @PostMapping(value="/create")
    public Result<Void> create(
            @RequestBody UserPostSaveDto userPostSaveDto) {
        userPostSaveDto.validated();
        UserPostCreateRegulation userPostCreateRegulation = new UserPostCreateRegulation(userPostMapper);
        userPostCreateRegulation.isSatisfiedBy(userPostSaveDto);
        if(userPostService.save(userPostSaveDto.to())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

    /**
     * 更新 系统岗位
     * @param userPostUpdateDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统岗位 信息")
    @PostMapping(value="/update")
    public Result<Void> updateById(
            @RequestBody UserPostUpdateDto userPostUpdateDto) {
        userPostUpdateDto.validated();
        UserPostUpdateRegulation userPostUpdateRegulation = new UserPostUpdateRegulation(userPostMapper);
        userPostUpdateRegulation.isSatisfiedBy(userPostUpdateDto);
        UserPost userPostOf = userPostService.getById(userPostUpdateDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(userPostUpdateDto.to(), userPostOf);
        if(userPostService.updateById(userPostOf)){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

}